//
//  RegistViewController.m
//  project
//
//  Created by 张亚林 on 2017/10/25.
//  Copyright © 2017年 Macmini. All rights reserved.
//

#import "RegistViewController.h"

#import "ConfriusernameViewController.h"
#import "SetnewPasswordViewController.h"
#import "ZWTools.h"
#import "MBProgressHUD+KR.h"
#import "STValidateCode.h"

@interface RegistViewController ()
@property (weak, nonatomic) IBOutlet UITextField *emailTF;

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;

@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"注册";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)codeBtn:(id)sender {
    
    ZWTools *tools = [ZWTools sharedManager];
    [tools getPhoneCodeFromNetWorkingID:self.phoneTF.text sucessBlock:^(id jsonResponse) {
        
        NSLog(@"sucess  %@", jsonResponse);
        [MBProgressHUD showSuccess:@"获取验证码成功"];
        [STValidateCode timeCuntDown:sender];
    } Error:^(id jsonResponse) {
        NSLog(@"error  %@", jsonResponse);
         [MBProgressHUD showError:@"获取验证码失败"];
    }];
}

- (IBAction)clickRegist:(id)sender {
    
    NSMutableDictionary *requestParameter = [NSMutableDictionary dictionary];
    requestParameter[@"email"] = self.emailTF.text;
    requestParameter[@"phone"]= self.phoneTF.text;
    requestParameter[@"msgCode"] = self.codeTF.text;
    requestParameter[@"password"] = self.passwordTF.text;
    
    ZWTools *tools = [ZWTools sharedManager];
    [tools regist:requestParameter sucessBlock:^(id jsonResponse) {
        
        ZWUserModel *model = [ZWUserModel parse:jsonResponse[@"data"]];
        model.token = token;
        loginuserInfo = model;
        [NSKeyedArchiver archiveRootObject:loginuserInfo toFile:PATH_USERINFO_DATA];
        
        
        NSLog(@"sucess  %@", jsonResponse);
        NSInteger result = [[jsonResponse objectForKey:@"code"] integerValue];
        if (result==0) {
            ConfriusernameViewController *confriusernameVC = [ConfriusernameViewController new];
            NSDictionary *data = [jsonResponse objectForKey:@"data"];
            confriusernameVC.userid = [[data objectForKey:@"id"] integerValue];
            NSLog(@"%ld", (long)confriusernameVC.userid);
            
            [self.navigationController pushViewController:confriusernameVC animated:YES];
        }
       
        
    } Error:^(id jsonResponse) {
        
        NSString *msg = [jsonResponse objectForKey:@"msg"];
        NSLog(@"sucess  %@", jsonResponse);
        [MBProgressHUD showSuccess:msg];
        
    }];
    

}

@end
